package com.flyme.module.exam.provider.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.constants.CommonConstants;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.utils.StringUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.exam.client.entity.ExamType;
import com.flyme.module.exam.client.enums.AbilityItemEnum;
import com.flyme.module.exam.provider.mapper.ExamTypeMapper;
import com.flyme.module.exam.provider.service.ExamTypeService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: flyme
 * @date: 2018/3/7 15:27
 * @desc: 题库管理接口实现
 */
@Service
public class ExamTypeServiceImpl extends BaseServiceImpl<ExamTypeMapper, ExamType> implements ExamTypeService {
    @Override
    public Boolean insert(ExamType examType) {
        String parentId = examType.getParentId();
        examType.setStatus(CommonConstants.ENABLED);
        examType.setParentId(StringUtils.isBlank(parentId) ? "0" : parentId);
        examType.setIsParent(0);
        if (!parentId.equals("0")) {
            ExamType parent = getById(parentId);
            parent.setIsParent(1);
            //更新父节点
            saveOrUpdate(parent);
        }
        return save(examType);
    }


    @Override
    public Boolean update(ExamType examType) {
        return saveOrUpdate(examType);
    }

    @Override
    public ResultBody delById(Long id) {
        ExamType examType = getById(id);
        String parentId = examType.getParentId();
        if (examType.getIsParent().equals(1)) {
            int n = count(q().eq("parentId", id));
            if (n > 0) {
                return ResultBody.failed("请先删除子分类");
            }
        } else {
            int n = count(q().eq("parentId", parentId));
            if (n == 1) {
                UpdateWrapper u = new UpdateWrapper();
                update(u.set(true, "isParent", 0).eq(true, "id", parentId));
            }
        }
        removeById(id);
        return ResultBody.success("刪除成功");
    }

    /**
     * 根据code查询项目
     */
    public ExamType findByCode(String code){
        return getOne(q().eq(true, "examTypeCode", code), false);
    }

    /**
     * 查询所有项目
     */
    public List<ExamType> selectAll(){
        return list(q().orderByAsc("sortOrder"));
    }

    /**
     * 雷达图项目
     */
    public List<EntityMap> selectFound(Long accountInfoId, List<String> items){
        PagerInfo pagerInfo = new PagerInfo();
        pagerInfo.setPageNumber(1);
        pagerInfo.setPageSize(100);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("et.id abilityitemId,et.examTypeName abilityitem,et.examTypeCode abilityitemCode,d.duanName,d.duanIcon,"
                + "u.abilityScore score, RIGHT(duanName, 2) gradeName," +
            "CASE WHEN d.duanLevel = 'bronze' THEN u.abilityScore * 0.04 WHEN d.duanLevel = 'silver' THEN u.abilityScore * 0.0267 " +
            "WHEN d.duanLevel = 'gold' THEN u.abilityScore * 0.01875 WHEN d.duanLevel = 'platinum' THEN u.abilityScore * 0.01333 " +
            "ELSE u.abilityScore * 0.01 END rait");
        queryWrapper.notIn("et.examTypeCode", items);
        queryWrapper.eq("u.userId", accountInfoId);
        queryWrapper.orderByAsc("et.sortOrder");
        queryWrapper.createAlias("userability", "u");
        queryWrapper.createAlias("duan", "d");
        return (List<EntityMap>) pageList(queryWrapper).getRows();
    }

    /**
     * 查询项目
     * @return
     */
    public EntityMap selectMap(){
        QueryWrapper qw = new QueryWrapper<Map<String, Object>>();
        qw.select("CONCAT(examTypeName,'能力') examTypeName", "id");
        List<Map<String, Object>> list = listMaps(qw);
        EntityMap map = new EntityMap();
        for (Map<String, Object> stringObjectMap : list) {
            map.put(stringObjectMap.get("examTypeName").toString(), stringObjectMap.get("id"));
        }
        return map;
    }


    /**
     * 查询除学历，工作及综合以外的项目id
     */
    public List<ExamType> selectOthers(){
        List<ExamType> list = list(q().ne("examTypeCode", AbilityItemEnum.education.code)
                .ne("examTypeCode", AbilityItemEnum.work.code)
                .ne("examTypeCode", AbilityItemEnum.synthesis.code));
        return list;
    }
}
